package cn.alcatraz.mq_02_sync;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * @Description: 生产者
 * @Author: Alcatraz
 * @Version: 1.0
 * @Date:  2022/7/26 21:11
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        // 创建生产者
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("sync_producer_group");

        // 连接nameserver
        defaultMQProducer.setNamesrvAddr("127.0.0.1:9876");

        // 启动生产者
        defaultMQProducer.start();

        // 发送异步消息
        Message message = new Message("sync_topic", "sync", "测试异步消息".getBytes(StandardCharsets.UTF_8));
        defaultMQProducer.send(message, new SendCallback() {
            /**
             * 消息发送成功
             * @param sendResult
             */
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            /**
             * 消息发送失败
             * @param throwable
             */
            @Override
            public void onException(Throwable throwable) {
                System.out.println(throwable);
            }
        });
    }
}
